describe("promise Test", function() {
    var p, js;

    beforeEach(function() {
        p = promise();
        js = jasmine.createSpy();
        jasmine.clock().install();
    });

    afterEach(function() {
        jasmine.clock().uninstall();
    });
    it(" can be complete and then ", function() {
        p.complete("abc");

        p.then(function(value) {
            js(value);
            expect(js).toHaveBeenCalledWith("abc");
        });

    });

    it("can be then and complete", function() {
        p.then(function(value) {
            js(value);
            return value;
        });
        // console.log(p.solve("abc"));
        setTimeout(function() {
            p.complete("abc");
        }, 1000);
        jasmine.clock().tick(1010);
        expect(js).toHaveBeenCalledWith("abc");


    });
    it("can then more than 1 times", function() {
        p.then(function(value) {
            js(value);
            expect(value).toEqual("abc");
            return "abcd";
        }).then(function(value) {
            js(value);
            expect(value).toEqual("abcd");
        });
        setTimeout(function() {
            p.complete("abc");
        }, 1000);
        jasmine.clock().tick(1020);
        expect(js.calls.count()).toEqual(2);

    });
    it("if complete err, then will not exec", function() {
        p.complete(function() {
            throw new Error();
        }).then(function(value) {
            js(value);
        });
        jasmine.clock().tick(100);
        expect(js.calls.count()).toEqual(0);
    });
    it('if solve err ,error will exec', function() {
        p.complete(function() {
            throw new Error();
        }).error(function(e) {
            js(e);
        });
        jasmine.clock().tick(100);
        expect(js.calls.count()).toEqual(1);
    });
    it('if then err ,the last then will not exec and error will be called', function() {
        p.complete('ok').then(function(value) {
            throw new Error();
        }).then(function(v) {
            js(v);
        });
        jasmine.clock().tick(100);
        expect(js.calls.count()).toEqual(0);
        p.error(function(e) {
            js();
        });
        jasmine.clock().tick(100);
        expect(js.calls.count()).toEqual(1);
    });
    it('wait will wait for some time then exec', function() {
        p.wait("abc", 1000).then(function(value) {
            js(value);
        })
        expect(js.calls.count()).toEqual(0);
        jasmine.clock().tick(1100);
        expect(js.calls.count()).toEqual(1);
    })

});